﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Compiler
{
    /// <summary>
    /// 文法分析辅助操作 赶进度，不实现了
    /// </summary>
    public class SyntaxHelper
    {
        public static List<KeyValuePair<string, string>> mProductions;
        public static Dictionary<string, List<int>> mTable;
        public static Dictionary<string, int> mTerminator;
        /// <summary>
        /// 消除左递归后的产生式
        /// </summary>
        public static List<KeyValuePair<string, string>> NewProductions = new List<KeyValuePair<string, string>>();

        public static void ProductionToAnalyzeTable()
        {
            mProductions = Production.Productions;
            mTable = PredictiveAnalyzeTable.Table;
            mTerminator = PredictiveAnalyzeTable.Terminator;

            EliminateLeftRecursion();
            CalculateFirstSets();
            CalculateFollowSets();
            CalculateAnalyzeTable();

            foreach (var pro in NewProductions)
            {
                Compiler.WriteLine($"*{pro.Key} {pro.Value}");
            }
        }

        /// <summary>
        /// 消除左递归
        /// </summary>
        public static void EliminateLeftRecursion()
        {
            mProductions.GroupBy(o => o.Key).ToList().ForEach(o =>
            {
                var left = o.Key;
                var multiRights = o.Select(oo => oo.Value.Split().ToList()).ToList();

                List<List<string>> b = new List<List<string>>();
                List<List<string>> Aa = new List<List<string>>();

                Compiler.WriteLine("--asda");

                foreach (var rights in multiRights)
                {
                    if (left == rights[0])
                    {
                        Aa.Add(rights);
                    }
                    else
                    {
                        b.Add(rights);
                    }
                }
                if (Aa.Count == 0) return;
                for (int i = 0; i < b.Count; i++)
                {
                    b[i].Add($"{left}'");
                    NewProductions.Add(new KeyValuePair<string, string>(left, string.Join(" ", b[i])));
                }
                for (int i = 0; i < Aa.Count; i++)
                {
                    Aa[i].RemoveAt(0);
                    Aa[i].Add($"{left}'");
                    NewProductions.Add(new KeyValuePair<string, string>($"{left}'", string.Join(" ", Aa[i])));
                }
                NewProductions.Add(new KeyValuePair<string, string>($"{left}'", "$"));
            });
        }
        /// <summary>
        /// 求FIRST集
        /// </summary>
        public static void CalculateFirstSets()
        {

        }
        /// <summary>
        /// 求FOLLOW集
        /// </summary>
        public static void CalculateFollowSets()
        {

        }
        /// <summary>
        /// 计算预测分析表
        /// </summary>
        public static void CalculateAnalyzeTable()
        {

        }
    }
}
